package org.csu.mypetstorems.controller;

import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.csu.mypetstorems.entity.Admin;
import org.csu.mypetstorems.entity.PageBean;
import org.csu.mypetstorems.result.Result;
import org.csu.mypetstorems.service.AdminService;
import org.csu.mypetstorems.util.JwtUtil;
import org.csu.mypetstorems.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
public class AdminController{
    @Autowired
    private AdminService adminService;
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/admin/login")
    public Result<Object> login(String username, String password){
        if(adminService.signOn(username, password)){
            //加入JWT令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("username",username);
            claims.put("password",password);
            String token = JwtUtil.genToken(claims);
            //把token存储到redis中，
//            ValueOperations<String,String> operations = stringRedisTemplate.opsForValue();
//            operations.set(token,token,12, TimeUnit.HOURS);
            return Result.success(token);
        }else{
            return Result.error("用户名或密码错误");
        }
    }

    @PostMapping("/admin/register")
    public Result<String> register(@Valid @RequestBody Admin admin , BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if(adminService.addAdmin(admin))
            return Result.success();
        else
            return Result.error("注册失败");
    }

    @PostMapping("/admin")
    public Result<String> adminAddService(@Valid @RequestBody Admin admin, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if(adminService.addAdmin(admin))
            return Result.success();
        else
            return Result.error("用户名已存在");
    }

    @PutMapping("/admin")
    public Result<String> adminEditService(@Valid @RequestBody Admin admin, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if(adminService.updateAdmin(admin))
            return Result.success();
        else
            return Result.error("更新admin失败");
    }

    @DeleteMapping("/admin")
    public Result<String> adminDeleteService(@RequestParam String id){
        if(adminService.deleteAdmin(id))
            return Result.success();
        else
            return Result.error("删除admin失败");
    }

    @GetMapping("/admin")
    public Result<PageBean<Admin>> adminListWithConditionService(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String nameForSearch
    ){
        PageBean<Admin> pageBean = adminService.getAdminWithCondition(pageNum, pageSize, nameForSearch);
        if (pageBean == null)
            return Result.error("获取admin条件查询失败");
        else
            return Result.success(pageBean);
    }

    @GetMapping("/admin/adminInfo")
    public Result<String> adminGetByUsername(){
        Map<String, Object> map = ThreadLocalUtil.get();
        return Result.success(map.get("username").toString());
    }
}
